package registcenter;
import com.xf.common.Invocation;
import com.xf.protocol.HttpServer;
import com.xf.rigister.RemoteRigister;

import java.io.IOException;

public class RegistCenter {
    public static void main(String[] args) {
        if (args.length != 6) {
            System.out.println("使用示例: java -jar ConfigApp.jar -r <rigistcenter> -p1 <port1> -p2 <port2>");
            return;
        }
        String Address=null;//注册中心ip地址
        Integer port1=null;//注册中心开放给服务器的端口
        Integer port2=null;//注册中心开放给客户端的端口
        for (int i = 0; i < args.length; i++) {
            switch (args[i]) {
                case "-r":
                    if (i + 1 < args.length) {
                        Address = args[++i];
                    } else {
                        System.out.println("错误: 没有提供注册中心ip地址");
                        return;
                    }
                    break;
                case "-p1":
                    if (i + 1 < args.length) {
                        try {
                            port1 = Integer.parseInt(args[++i]);
                        } catch (NumberFormatException e) {
                            System.out.println("错误: 端口号1必须是整数");
                            return;
                        }
                    } else {
                        System.out.println("错误: 没有提供端口号1");
                        return;
                    }
                    break;
                case "-p2":
                    if (i + 1 < args.length) {
                        try {
                            port2 = Integer.parseInt(args[++i]);
                        } catch (NumberFormatException e) {
                            System.out.println("错误: 端口号2必须是整数");
                            return;
                        }
                    } else {
                        System.out.println("错误: 没有提供端口号2");
                        return;
                    }
                    break;
                default:
                    System.out.println("未知命令: " + args[i]);
                    return;
            }
        }
        HttpServer httpServer = new HttpServer();
        try {
            System.out.println("注册中心启动中。。。");
            System.out.println("注册中心启动成功！");
            httpServer.start_for_rigister(Address,port1,port2);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
